package com.chase.springboot.service.impl;

import com.chase.springboot.mapper.RoleMapper;
import com.chase.springboot.mapper.RoleMenuMapper;
import com.chase.springboot.mapper.UserRoleMapper;
import com.chase.springboot.model.Role;
import com.chase.springboot.service.RoleMenuService;
import com.chase.springboot.service.RoleService;
import com.chase.springboot.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public boolean add(Role role) {
        int i = roleMapper.insert(role);
        return i > 0;
    }

    @Transactional  //
    @Override
    public boolean deleteById(int id) {
        //删除关联
        Boolean b = userRoleMapper.deleteByRoleId(id);
        int i2 = roleMenuMapper.deleteByRoleId(id);
        //删除角色
        int i = roleMapper.deleteByPrimaryKey(id);
        return i > 0 && b && i2 > 0;
    }
}
